package com.lsw.dialog;

import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.lsw.R;
import com.lsw.util.InputTools;


/**
 * 支付密码弹出框
 *
 * @author lsw
 * @Date 2016年10月22日下午1:21:06
 */
public class PasswordPayDialogUtil {
    private Context context;
    private EditText etPassword;
    /**
     * 支付密码校验监听
     */
    private OnPasswordValidateListener listener;

    private boolean isNeedShowCrowdfunding = false;

    public PasswordPayDialogUtil(Context context,
                                 OnPasswordValidateListener listener) {
        this.context = context;
        this.listener = listener;
    }

    /**
     * @param context
     * @param isNeedShowCrowdfunding 是否显示众筹金额--关于商城的内容这个必须为true
     * @param listener
     */
    public PasswordPayDialogUtil(Context context,
                                 boolean isNeedShowCrowdfunding, OnPasswordValidateListener listener) {
        this.context = context;
        this.listener = listener;
        this.isNeedShowCrowdfunding = isNeedShowCrowdfunding;
    }

    private ActionSheetDialogUtil dialogUtil;

    /**
     * 显示字体设置对话框
     */
    public void showPayPasswordDialog() {
        if (dialogUtil == null) {
            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View contentView = inflater.inflate(R.layout.dialog_password_pay,
                    null);
            etPassword = (EditText) contentView.findViewById(R.id.etPassword);
            etPassword.addTextChangedListener(new TextWatcher() {
                @Override
                public void onTextChanged(CharSequence arg0, int arg1,
                                          int arg2, int arg3) {

                }

                @Override
                public void beforeTextChanged(CharSequence arg0, int arg1,
                                              int arg2, int arg3) {

                }

                @Override
                public void afterTextChanged(Editable arg0) {
                    if (arg0.length() == 6) {
                        // 输入完成，校验支付密码
                        validatePassword(arg0.toString());
                    }
                }
            });
            // 关闭
            contentView.findViewById(R.id.btnClose).setOnClickListener(
                    new View.OnClickListener() {
                        @Override
                        public void onClick(View arg0) {
                            dialogUtil.dismiss();
                        }
                    });
            dialogUtil = new ActionSheetDialogUtil(context, contentView);
        }
        etPassword.setFocusable(true);
        etPassword.getEditableText().clear();
        dialogUtil.show();
    }

    /**
     * @param password
     */
    private void validatePassword(final String password) {
        InputTools.HideKeyboard(etPassword);
        if (true) {
            // 支付密码正确
            if (listener != null) {
                listener.onPasswordValidate(true, password);
            }
            dialogUtil.dismiss();
        } else {
            // 支付密码不正确
            Toast.makeText(context, "支付密码不正确，请重新输入", Toast.LENGTH_LONG).show();
            etPassword.setText("");
        }
    }

    /**
     * 支付密码校验监听
     *
     * @author heiyue
     * @Email heiyue623@126.com
     * @Date 2016年10月22日下午2:24:19
     */
    public interface OnPasswordValidateListener {
        /**
         * @param success  支付密码校验成功
         * @param password 返回输入的输入的支付密码【未加密】
         */
        public void onPasswordValidate(boolean success, String password);
    }
}
